package com.ts.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class LinkedHashMap2ObjectUtil<T> {

    public T linkedHashMap2Object(HashMap map,Class clazz){

        Object obj = null;
        try {
            obj = clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


        Method[] methods = clazz.getMethods();
        Iterator<String> iterator = map.keySet().iterator();
        while(iterator.hasNext()){
            String key = iterator.next();
            for (Method method : methods) {
                if(method.getName().startsWith("set")){
                    try {
                        Object invoke = map.get(key);
                        method.invoke(obj, invoke);
                    }  catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }
}
